package sn_gm_report

import org.DataUtils

class MonitoringController {
    def dynamicQueryService
    def commonDataService
    def exportFileService

    def monitoring() {

        def pageCurrent = params.pageCurrent ? Integer.parseInt(params.pageCurrent) : 1
        def pageSize = params.pageSize ? Integer.parseInt(params.pageSize) : 10
        if (params.export == 'xls') {
            params.startno = 1
            params.endno = 56800
        } else {
            params.startno = (pageSize * (pageCurrent - 1)) + 1
            params.endno = pageSize * pageCurrent
        }
        def procedureTeam = commonDataService.procedureTeam(params)
        def procedure_IdList = commonDataService.procedure_IdList(params)
        params.SYS_QUERY_NAME = 'monitoringList'
        def monitoringList = dynamicQueryService.queryForList(params)
        def tableHeadList = []
        if (monitoringList.size() > 0) {
            tableHeadList = monitoringList[0].keySet()
        }
        params.SYS_QUERY_NAME = 'monitoringCount'
        def totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT

        if (params.export == 'xls') {
            def str = g.render(template: "${params.EXPORT_CONTENT_NAME}", model: [monitoringList: monitoringList, tableHeadList: tableHeadList])
            exportFileService.exportExcelbyWeb(response, "${params.SYS_QUERY_NAME}", str)
        } else {
            [pageSizeList  : DataUtils.getMonPageSizeList(), pageSize: pageSize, pageCurrent: pageCurrent, monitoringList: monitoringList,
             tableHeadList : tableHeadList, totalCount: totalCount, procedure_IdList: procedure_IdList, procedure: params.procedure ?: '',procedureTeam: procedureTeam,
             startno       : params.startno, endno: params.endno, batchStart: params.batchStart ?: '', batchEnd: params.batchEnd ?: '',team: params.team ?: '',
             lastBatchStart: params.lastBatchStart ?: '', lastBatchEnd: params.lastBatchEnd ?: '', runStart: params.runStart ?: '',
             runEnd        : params.runEnd ?: '', procedureStart: params.procedureStart ?: '', procedureEnd: params.procedureEnd ?: '']

        }
    }

    def realMonitoring() {

        def pageCurrent = params.pageCurrent ? Integer.parseInt(params.pageCurrent) : 1
        def pageSize = params.pageSize ? Integer.parseInt(params.pageSize) : 10
        if (params.export == 'xls') {
            params.startno = 1
            params.endno = 56800
        } else {
            params.startno = (pageSize * (pageCurrent - 1)) + 1
            params.endno = pageSize * pageCurrent
        }

        def procedure_IdList = commonDataService.procedure_IdList(params)
        def procedureTeam = commonDataService.procedureTeam(params)
        params.SYS_QUERY_NAME = 'realMonitoringList'
        def realMonitoringList = dynamicQueryService.queryForList(params)
        def tableHeadList = []
        if (realMonitoringList.size() > 0) {
            tableHeadList = realMonitoringList[0].keySet()
        }
        params.SYS_QUERY_NAME = 'realMonitoringCount'
        def totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
        print params
        if (params.export == 'xls') {
            def str = g.render(template: "${params.EXPORT_CONTENT_NAME}", model: [realMonitoringList: realMonitoringList, tableHeadList: tableHeadList])
            exportFileService.exportExcelbyWeb(response, "${params.SYS_QUERY_NAME}", str)
        } else {
            [pageSizeList  : DataUtils.getPageSizeList(), pageSize: pageSize, pageCurrent: pageCurrent, realMonitoringList: realMonitoringList,
             tableHeadList : tableHeadList, totalCount: totalCount, procedure_IdList: procedure_IdList, procedure: params.procedure ?: '',
             startno       : params.startno, endno: params.endno, batchStart: params.batchStart ?: '', batchEnd: params.batchEnd ?: '', procedureTeam: procedureTeam,
             lastBatchStart: params.lastBatchStart ?: '', lastBatchEnd: params.lastBatchEnd ?: '', runStart: params.runStart ?: '', team: params.team ?: '',
             runEnd        : params.runEnd ?: '', procedureStart: params.procedureStart ?: '', procedureEnd: params.procedureEnd ?: '']
        }
    }
}
